package com.yourwork.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.yourwork.dao.TypeManagementDao;
import com.yourwork.dao.ServiceProjectDao;
import com.yourwork.model.ServiceProject;
import com.yourwork.model.TypeManagement;
import com.yourwork.service.ServiceProjectService;

@Service(value = "serviceItemService")
public class ServiceProjectServiceImp implements ServiceProjectService {
	@Autowired
	@Qualifier(value = "serviceItemDao")
	private ServiceProjectDao serviceItemDao;
	@Autowired
	@Qualifier(value = "employeeTypeDao")
	private TypeManagementDao employeeTypeDao;

	// 查询id
	public ServiceProject queryServiceItem(int id) {
		// TODO Auto-generated method stub
		try {
			return serviceItemDao.queryServiceItem(id);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}

	public List<TypeManagement> queryEmployeeType() {
		// TODO: handle exception
		try {
			return employeeTypeDao.queryEmployeeType(1);
		} catch (Exception e) {
			return null;
		}
	}

	public List<ServiceProject> queryServiceItemAll(boolean status) {
		// TODO Auto-generated method stub
		try {
			List<ServiceProject> siList = serviceItemDao.queryServiceItemAll(status);// 得到siList对象
			return siList;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}

	public String insertServiceItem(ServiceProject serviceItem, int[] typeId) {
		// TODO Auto-generated method stub
		try {
			String typeName = "";
			for (int i = 0; i < typeId.length; i++) {// 多选框传入的值 转换为String类型
				typeName = typeName + typeId[i];// 字符串拼接
				if (i != typeId.length - 1) {
					typeName = typeName + ",";
				}
			}
			serviceItem.setType(typeName);
			serviceItem.setStatus(true);
			serviceItemDao.insertServiceItem(serviceItem);
			return "执行操作成功";
		} catch (Exception e) {
			// TODO: handle exception
			return "执行操作失败";
		}

	}

	public String updateStatus(ServiceProject serviceItem) {
		// TODO Auto-generated method stub
		try {
			serviceItem = serviceItemDao.queryServiceItem(serviceItem.getId());
			boolean status = serviceItem.getStatus();
			if (status) {
				status = false;
			} else {
				status = true;
			}
			System.out.println(status);
			serviceItem.setStatus(status);
			serviceItemDao.updateStatus(serviceItem);
			return "修改成功";
		} catch (Exception e) {
			return "修改失败";
		}
	}

	// 分页查询
	public List<ServiceProject> queryServiceItemAllF(int page, int size, boolean status) {
		// TODO Auto-generated method stub
		try {
			int count;
			count = serviceItemDao.countServiceItem(status);
			// 判断page的值
			if (page <= (count / size)) {
				page = page + 0;
			} else {
				if (count % size == 0) {
					page = count / size;
				} else {
					page = count / size + 1;
				}
			}
			Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");
			List<ServiceProject> siList = serviceItemDao.queryServiceItemAllF(status, pageable);// 得到siList对象
			return siList;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}
	// 判断最大页数
	public int queryMaxPage(int page, int size, boolean status) {
		// TODO Auto-generated method stub
		int count;
		count = serviceItemDao.countServiceItem(status);
		if (count % size == 0) {
			page = count / size - 1;
		} else {
			page = count / size;
		}
		return page;
	}
}
